home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471323519
Publication Date: 11/01/98

Search this book:
 
Previous Table of Contents Next


Part Four
Error Handling

In a complicated program, it is extremely difficult to remove absolutely every bug. You may test a program for a long time without finding any new bugs, but you can never be completely sure you have found them all. That means the program’s final compiled version must be prepared to handle bugs that arise unexpectedly.

In addition, even a perfectly debugged program remains subject to random occurrences in the Windows environment. Other programs may exhaust the system’s memory so your program cannot run properly. The user may accidentally delete an important file or part of the system registry. These situations create errors the program must handle, but they are not bugs.

The chapters in Part 4 explain runtime error handling topics. They show how to detect and manage bugs and unexpected conditions that might otherwise crash the program. Using proper error handling techniques, a program can continue to operate even when it runs into unanticipated errors.

CHAPTER 12
Error Handling Fundamentals

This chapter explains the family of On Error statements Visual Basic uses to handle errors. It tells how a program installs and removes error handlers, and it explains some of the idiosyncrasies of error handling code. After reading this chapter you will be able to write basic error handlers to protect your programs from the unexpected.

Use On Error

A Visual Basic program uses the On Error statement to register error handling code. This statement can take one of three forms:

•  On Error GoTo 0
•  On Error Resume Next
•  On Error GoTo line

These forms tell Visual Basic what it should do when the program encounters an error. The three forms are described in the following sections.

On Error GoTo 0

On Error GoTo 0 is relatively straightforward. It simply cancels any currently installed error handler assigned by a previous On Error GoTo line or On Error Resume Next. If the program encounters an error after this statement executes, it crashes.

On Error Resume Next

On Error Resume Next makes the program ignore errors. When it encounters an error, the program continues execution after the statement that caused the error.

When a program uses On Error Resume Next, it should check the Err object after every operation that might cause an error. If the value Err.Number is nonzero, the operation caused an error and the program can take special action.

The program should check Err.Number immediately after the statement in question. Certain other actions reset the Err object and remove the previous error information.

Many programs use On Error GoTo Next when they present a common dialog to the user. The CommonDialog control’s CancelError property indicates whether the control should raise an error if the user cancels the dialog. The following code fragment shows how a program can use CancelError to decide whether to continue an action such as loading a file.

' Generate an error if the user cancels.
dlgOpenFile.CancelError = True

' Ignore errors for now.
On Error Resume Next

' Present the dialog.
dlgOpenFile.ShowOpen

' See if there was an error.
If Err.Number = cdlCancel Then
    ' The user canceled. Do nothing.
    Exit Sub
ElseIf Err.Number <> 0 Then

    ' Unknown error. Take more action.
        :
End If

' Resume normal error handling.
On Error GoTo 0

On Error GoTo Line

The On Error GoTo line statement registers a new error handler. If the program encounters an error, it passes control to the error handler beginning at the indicated line number or label. The error handler can then take appropriate action.

The following code shows a simple error handler that catches unexpected errors and describes them to the user.

Private Sub DoSomething()
    ' Install the error handler.
    On Error GoTo UnexpectedError

    ' Do stuff.
        :

    ' Do not pass through into the error handler code.
    Exit Sub

UnexpectedError:
    ' Describe the error to the user.
    MsgBox "Unexpected error" & _
        Str$(Err.Number) & _
        " in subroutine DoSomething." & _
        vbCrLf & _
        Err.Description
    Exit Sub
End Sub

Leave Error Handlers

There are several ways a program can leave error handling code and return to normal execution.

•  Resume
•  Resume Next
•  Exit Sub/Function/Property
•  End Sub/Function/Property
•  Err.Raise